

import { Transform, Type } from "class-transformer";
import { IsArray, IsBoolean, IsDate, IsEnum, IsIn, IsNumber, IsOptional, IsString, ValidateNested } from "class-validator";
import { LangEnum } from "src/common/enums";
import { AudioRateEnum } from "src/common/enums/audio.enum";
import { PictorialEnum } from "../../picture/enum";
import { RateFileInfoDto } from "../../rate_file_info/dto";
import { VideoForm } from "../../video/dto/create-video.dto";

class Video {
    // @IsOptional()
    // @IsUUID("all", { message: "视频文件id必须是UIId" })
    id?: string;

    url?: string;
    //视频、图片高度
    @IsNumber({ allowNaN: false }, { message: "视频高度格式不正确" })
    height?: number;
    //视频、图片宽度
    @IsNumber({ allowNaN: false }, { message: "视频宽度格式不正确" })
    width?: number;
    //视频时长
    @IsNumber({ allowNaN: false }, { message: "视频长度格式不正确" })
    duration?: number;
    //存储名称
    @IsString({ message: "存储文件名称格式不正确" })
    objectName?: string;
}

export class SongForm {

    /**
   * 歌曲ID
   */
    @IsOptional()
    @IsNumber({ allowNaN: false }, { message: "歌曲id格式不正确"})
    id?: number;
    /**
     * 歌手ID列表
     */
    @IsOptional()
    @IsArray({ message: "歌手id格式不是数组格式" })
    @IsNumber({ allowNaN: false }, { message: "歌手id格式不正确", each: true })
    artistIds: number[];

    /**
     * 歌手ID列表
     */
    @IsOptional()
    @IsArray({ message: "专辑id格式不是数组格式" })
    @IsNumber({ allowNaN: false }, { message: "专辑id格式不正确", each: true })
    albumIds: number[];

    //歌曲标题
    @IsOptional()
    @IsString({ message: "歌曲标题必填，且为字符串" })
    title: string;
    //歌曲标题
    @IsOptional()
    @IsString({ message: "歌曲简介必填，且为字符串" })
    introduce: string;
    //标签
    @IsOptional()
    genre?: number | string;

    //封面图
    @IsOptional()
    @IsArray({ message: "歌曲图片不是数组格式" })
    @ValidateNested({ each: true })
    @Type(() => MusicUploadFile)
    pic: MusicUploadFile[];

    //视频
    @ValidateNested()
    @Type(() => VideoForm)
    video?: VideoForm;

    // @IsEnum(AudioRateEnum, { message: "音质列表不是数组格式", each: true })
    rateList: AudioRateEnum[];

    //发行日期
    @Transform(v => new Date(v))
    @IsDate()
    releaseDate: Date;
    //下架时间
    @Transform(v => new Date(v))
    @IsDate()
    downTime: Date;
    //上架时间
    @Transform(v => new Date(v))
    @IsDate()
    pushTime: Date;
    //语言
    @IsEnum(LangEnum, { message: "语言格式不正确" })
    lang: LangEnum;
    //歌词
    @IsString()
    lyric: string;
    //种类是否激活
    @IsBoolean()
    isActive: boolean;
    //是否是VIP歌曲
    @IsBoolean()
    isVip: boolean;
    /**
     * 排序
     */
    @Transform(v => +v)
    @IsNumber()
    sort: number;

    @IsArray({ message: "音质格式不正确222222222322232" })
    @ValidateNested({ each: true })
    @Type(() => RateFileInfoDto)
    rateFileInfos: RateFileInfoDto[];

}

export class FavoriteForm {
    @IsNumber()
    id: number;
    @IsNumber()
    songId: number;
}

export type UploadStatus = 'ready' | 'uploading' | 'success' | 'fail';
export interface UploadFile {
    name: string;
    percentage?: number;
    status: UploadStatus;
    size?: number;
    response?: unknown;
    uid: number;
    url?: string;
    raw?: UploadRawFile;
}

export interface UploadRawFile extends File {
    uid: number;
}

export class MusicUploadFile implements UploadFile {
    @IsString({ message: "文件名称格式不正确" })
    name: string;
    @IsOptional()
    @IsNumber({ allowNaN: false }, { message: "文件上传进度格式不正确" })
    percentage?: number;
    @IsIn(['ready', 'uploading', 'success', 'fail'])
    status: UploadStatus;

    @IsOptional()
    @IsNumber({ allowNaN: false }, { message: "文件size格式不正确" })
    size?: number;

    @IsOptional()
    response?: unknown;

    @Transform(v => +v)
    @IsNumber({ allowNaN: false }, { message: "文件uid格式不正确" })
    uid: number;

    @IsOptional()
    @IsString({ message: "文件url格式不正确" })
    url?: string;

    @IsOptional()
    raw?: UploadRawFile;

    @IsOptional()
    @IsString({ message: "文件id格式不正确" })
    id?: string;

    @IsOptional()
    @IsEnum(PictorialEnum, { message: "相册格式不正确" })
    picType?: PictorialEnum;
}
